package ir.values.users.instructions.mem;

import frontend.visitor.symboltbl.scope.Scope;
import ir.types.datatypes.PointerType;
import ir.types.datatypes.VoidType;
import ir.values.Value;
import ir.values.users.instructions.InstOperator;
import ir.values.users.instructions.Instruction;

public class StoreInst extends Instruction {
    // store type %name, ptype %name
    private final Value valueOperand;
    private final Value pointerOperand;

    public StoreInst(Value valueOperand, Value pointerOperand) {
        super(null, VoidType.VOID);
        this.valueOperand = valueOperand;
        this.pointerOperand = pointerOperand;
        operands.add(valueOperand);
        operands.add(pointerOperand);
    }
    @Override
    public boolean shouldGenerateId() {
        return false;
    }
    public PointerType getPointerType() {
        return (PointerType) pointerOperand.getType();
    }

    @Override
    public InstOperator getInstOperator() {
        return InstOperator.store;
    }

    @Override
    public String parseLLVM() {
        // eg: store i32 %3, i32* %2
        StringBuilder sb = new StringBuilder();
        sb
                .append(getInstOperator())
                .append(" ")
                .append(getPointerType().getBaseType().toString())
                .append(" ")
                .append(valueOperand.parseUsingName())
                .append(", ")
                .append(getPointerType().toString())
                .append(" ")
                .append(pointerOperand.parseUsingName());
        return sb.toString();
    }
}
